package logic

import (
	"context"
	"errors"
	"novel-hub/models/mysql"

	"novel-hub/novelrpc/internal/svc"
	"novel-hub/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelAddLogic {
	return &NovelAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelAddLogic) NovelAdd(in *novelrpc.NovelAddRequest) (*novelrpc.NovelAddResponse, error) {
	// todo: add your logic here and delete this line

	novel := mysql.Novel{
		Title:    in.Title,
		Category: in.Category,
		Label:    in.Label,
		Intro:    in.Intro,
	}
	if in.Title == "" || in.Category == "" || in.Intro == "" {
		return nil, errors.New("标题、分类、简介必填")
	}
	if err := novel.NovelAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &novelrpc.NovelAddResponse{
		NovelId: int64(novel.ID),
	}, nil
}
